*File for picking combinations of BFI personality indicators, from 1 to 12 

cd ""
clear 
set more off

use data_all_vars

* E
global ex "BFI2_1 BFI2_16 BFI2_31 BFI2_46 BFI2_6 BFI2_21 BFI2_36 BFI2_51 BFI2_11 BFI2_26 BFI2_41 BFI2_56 "
*C
global co "BFI2_3 BFI2_18 BFI2_33 BFI2_48 BFI2_8 BFI2_23 BFI2_38 BFI2_53 BFI2_13 BFI2_28 BFI2_43 BFI2_58"
* N
global ne "BFI2_4 BFI2_19 BFI2_34 BFI2_49 BFI2_9 BFI2_24 BFI2_39 BFI2_54 BFI2_14 BFI2_29 BFI2_44 BFI2_59"
* A
global ag "BFI2_2 BFI2_17 BFI2_32 BFI2_47 BFI2_7 BFI2_22 BFI2_37 BFI2_52 BFI2_12 BFI2_27 BFI2_42 BFI2_57"
* O
global op = "BFI2_10 BFI2_25 BFI2_40 BFI2_55 BFI2_5 BFI2_20 BFI2_35 BFI2_50 BFI2_15 BFI2_30 BFI2_45 BFI2_60"


foreach trait in ex co ne ag op    {



	local myvarlist $`trait'

	local count : word count `myvarlist'

	forvalues i= 1/`count' {
		gen result`i'=.
	}

	generate levels = ""

	* largest number with a varlist of 12 (12c6)
	local combinations = 10000


	capture set obs `combinations'

	local start = 1

	local list1 = "`myvarlist'"

	di "`list1'"

	di `count'

	gen m=.
	gen w2_m=.

	local j=1
	local k=1
	local l=1
	local m=1
	local n=1
	local o=1
	local p=1
	local q=1
	local r=1
	local s=1
	local t=1

	forvalues i = 1/`count' {

		local var1: word 1 of `list1'
		
		replace m =`var1'
		replace w2_m=w2_`var1'
		
		corr m w2_m
		
		replace result1=`r(rho)' in `i'
		
		* removes the "depvar" from the list (it should not be paired with itself)
		
		local list1 : subinstr local list1 "`var1'" ""
		
		di "`list1'"
		
		local list2 = "`list1'"


			foreach var2 of local list1 {

				replace levels= "`var1' `var2'" in `start'
				
				replace m =`var1'+`var2'
				replace w2_m=w2_`var1'+w2_`var2'
				
				corr m w2_m
				
				replace result2=`r(rho)' in `j'
				
				local ++j

				local start = `start' + 1
				
				local list2 : subinstr local list2 "`var2'" ""
				
				local list3 = "`list2'"
				
				

				foreach var3 of local list2 {

					replace levels= "`var1' `var2' `var3'" in `start'
					
					replace m =`var1'+`var2'+`var3'
					replace w2_m=w2_`var1'+w2_`var2'+w2_`var3'
					
					corr m w2_m
					
					replace result3=`r(rho)' in `k'
					
					local ++k
				
					local start = `start' + 1
					
					local list3 : subinstr local list3 "`var3'" ""
					
					local list4 = "`list3'"							
				
				
				
					foreach var4 of local list3 {

						replace levels= "`var1' `var2' `var3' `var4'" in `start'

						replace m =`var1'+`var2'+`var3'+`var4'
						replace w2_m=w2_`var1'+w2_`var2'+w2_`var3' +w2_`var4'
						
						corr m w2_m
						
						replace result4=`r(rho)' in `l'
						
						local ++l
						
						local start = `start' + 1
						
						local list4 : subinstr local list4 "`var4'" ""
						
						local list5 = "`list4'"		
					
					
						foreach var5 of local list4 {

							replace levels= "`var1' `var2' `var3' `var4' `var5'" in `start'

							replace m =`var1'+`var2'+`var3'+`var4'+`var5'
							replace w2_m=w2_`var1'+w2_`var2'+w2_`var3' +w2_`var4'+w2_`var5'
							
							corr m w2_m
							
							replace result5=`r(rho)' in `m'
							
							local ++m
							
							local start = `start' + 1
							
							local list5 : subinstr local list5 "`var5'" ""
							
							local list6 = "`list5'"		
							
							
							foreach var6 of local list5 {

								replace levels= "`var1' `var2' `var3' `var4' `var5' `var6'" in `start'

								replace m =`var1'+`var2'+`var3'+`var4'+`var5'+`var6'
								replace w2_m=w2_`var1'+w2_`var2'+w2_`var3' +w2_`var4'+w2_`var5'+w2_`var6'
								
								corr m w2_m
								
								replace result6=`r(rho)' in `n'
								
								local ++n
								
								local start = `start' + 1
								
								local list6 : subinstr local list6 "`var6'" ""
								
								local list7 = "`list6'"		
								
								
								
								foreach var7 of local list6 {

									replace levels= "`var1' `var2' `var3' `var4' `var5' `var6' `var7'" in `start'

									replace m =`var1'+`var2'+`var3'+`var4'+`var5'+`var6'+`var7'
									replace w2_m=w2_`var1'+w2_`var2'+w2_`var3' +w2_`var4'+w2_`var5'+w2_`var6'+w2_`var7'
									
									corr m w2_m
									
									replace result7=`r(rho)' in `o'
									
									local ++o
									
									local start = `start' + 1
									
									local list7 : subinstr local list7 "`var7'" ""
									
									local list8 = "`list7'"		
									
									
									
									foreach var8 of local list7 {

										replace levels= "`var1' `var2' `var3' `var4' `var5' `var6' `var7' `var8'" in `start'

										replace m =`var1'+`var2'+`var3'+`var4'+`var5'+`var6'+`var7'+`var8'
										replace w2_m=w2_`var1'+w2_`var2'+w2_`var3' +w2_`var4'+w2_`var5'+w2_`var6'+w2_`var7'+w2_`var8'
										
										corr m w2_m
										
										replace result8=`r(rho)' in `p'
										
										local ++p
										
										local start = `start' + 1
										
										local list8 : subinstr local list8 "`var8'" ""
										
										local list9 = "`list8'"	
										
										
										
										foreach var9 of local list8 {

											replace levels= "`var1' `var2' `var3' `var4' `var5' `var6' `var7' `var8' `var9'" in `start'

											replace m =`var1'+`var2'+`var3'+`var4'+`var5'+`var6'+`var7'+`var8'+`var9'
											replace w2_m=w2_`var1'+w2_`var2'+w2_`var3' +w2_`var4'+w2_`var5'+w2_`var6'+w2_`var7'+w2_`var8'+w2_`var9'
											
											corr m w2_m
											
											replace result9=`r(rho)' in `q'
											
											local ++q
											
											local start = `start' + 1
											
											local list9 : subinstr local list9 "`var9'" ""
											
											local list10 = "`list9'"		
												
											foreach var10 of local list9 {

												replace levels= "`var1' `var2' `var3' `var4' `var5' `var6' `var7' `var8' `var9' `var10'" in `start'

												replace m =`var1'+`var2'+`var3'+`var4'+`var5'+`var6'+`var7'+`var8'+`var9'+`var10'
												replace w2_m=w2_`var1'+w2_`var2'+w2_`var3' +w2_`var4'+w2_`var5'+w2_`var6'+w2_`var7'+w2_`var8'+w2_`var9' +w2_`var10'
												
												corr m w2_m
												
												replace result10=`r(rho)' in `r'
												
												local ++r
												
												local start = `start' + 1
												
												local list10 : subinstr local list10 "`var10'" ""
												
												local list11 = "`list10'"
												
												
													
												foreach var11 of local list10 {

													replace levels= "`var1' `var2' `var3' `var4' `var5' `var6' `var7' `var8' `var9' `var10' `var11'" in `start'

													replace m =`var1'+`var2'+`var3'+`var4'+`var5'+`var6'+`var7'+`var8'+`var9'+`var10'+`var11'
													replace w2_m=w2_`var1'+w2_`var2'+w2_`var3' +w2_`var4'+w2_`var5'+w2_`var6'+w2_`var7'+w2_`var8'+w2_`var9' +w2_`var10' +w2_`var11'
													
													corr m w2_m
													
													replace result11=`r(rho)' in `s'
													
													local ++s
													
													local start = `start' + 1
													
													local list11 : subinstr local list11 "`var11'" ""
													
													local list12 = "`list11'"		
													
									
													foreach var12 of local list11 {

														replace levels= "`var1' `var2' `var3' `var4' `var5' `var6' `var7' `var8' `var9' `var10' `var11' `var12'" in `start'

														replace m =`var1'+`var2'+`var3'+`var4'+`var5'+`var6'+`var7'+`var8'+`var9'+`var10'+`var11'+`var12'
														replace w2_m=w2_`var1'+w2_`var2'+w2_`var3' +w2_`var4'+w2_`var5'+w2_`var6'+w2_`var7'+w2_`var8'+w2_`var9' +w2_`var10' +w2_`var11'+w2_`var12'
														
														corr m w2_m
														
														replace result12=`r(rho)' in `t'
														
														local ++t
														
														local start = `start' + 1
														
														local list12 : subinstr local list12 "`var12'" ""
														
														local list13 = "`list12'"		
														
										
													
													
													
													}												
													
												
												}												
												
												
												
											}										
																				
											
										}										
										
									}								
									
									
									
									
								}								
								
								
								
							}		
						}				
					}
				
				}	
				

			}

		}

	list levels if !missing(levels)
	sum result*, d

	capture gen mean=.
	capture gen median=.
	local i=1
	foreach var of varlist result* {
		sum `var',d
		replace mean=`r(mean)' in `i'
		replace median=`r(p50)' in `i'
		local ++i
		
	}
	rename (mean median) (`trait'_mean `trait'_median)
	
	drop result* levels m w2_m
}

* Inputs for Figure 1
br ex_mean  co_mean  ne_mean  ag_mean  op_mean 
